package servlet;

import com.alibaba.fastjson.JSONObject;
import entity.GameRoom;
import entity.User;
import service.GameRoomService;
import service.Message;
import service.PlayerService;
import service.UserService;
import top.RuntimeContextProvider;
import utils.Common;

import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "hallServlet", urlPatterns = "*.hall", loadOnStartup = 2)
public class HallServlet extends HttpServlet {

    private static UserService userService = RuntimeContextProvider.getContextProvider().getUserService();
    private static GameRoomService gameRoomService = RuntimeContextProvider.getContextProvider().getGameRoomService();
    private static PlayerService playerService = RuntimeContextProvider.getContextProvider().getPlayerService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=utf-8");

        HttpSession userSession = req.getSession();

        User user = userService.getUserInfo(userSession);
        //登录验证
        if(user != null) {
            //用户已经进入了房间
            if(playerService.getBySession(userSession) != null) {
                resp.getWriter().write(Message.JOINED_ROOM);
                return;
            }

            switch(req.getServletPath()) {
                case "/get.hall":
                    JSONObject json = gameRoomService.getRoomAbstractListJson();
                    if(!Common.isStringEmpty(user.getHeadPic())) json.put("userheadPic", user.getHeadPic());
                    resp.getWriter().write(json.toJSONString());
                    break;
                case "/create.hall": {
                    User u = userService.getUserInfo(userSession);
                    if (u != null) {
                        //gameRoomService.createRoom();
                        //resp.getWriter().write(Message.redirect(req.getContextPath() + "/battleRoom.html"));
                    } else {
                        resp.getWriter().write(Message.redirect(req.getContextPath() + "/login.html"));
                    }
                    break;
                } case "/join.hall": {
                    System.out.println("request join hall");
                    String roomId = req.getParameter("roomId");
                    GameRoom gr = gameRoomService.getRoom(Long.parseLong(roomId));
                    if(gr != null) {
                        if(!Common.isStringEmpty(gr.getPasswordHash())) {
                            String pass = req.getParameter("roomPass");
                            if(Common.isStringEmpty(pass)  ||  !pass.equals(gr.getPasswordHash())) {
                                resp.getWriter().write(Message.ENTER_ROOM_FAIL);
                                break;
                            }
                        }
                        req.getSession().setAttribute("roomId", roomId);
                        //Common重定向, 然后在PlayerServlet中查找加入房间
                        resp.getWriter().write(Message.redirect(req.getContextPath() + "/battle.html"));
                    }
                    break;
                }
            }
        } else {
            resp.getWriter().write(Message.redirect(req.getContextPath() + "/login.html"));
        }
    }
}
